<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div class='wrapper'></div>
    <script>
        /*
            5、 抽取公共组件
            comment:  抽象出公共组件Component
            todo:  函数组件
        */

        const createDOMFromString = (domString) => {
            const div = document.createElement('div')
            div.innerHTML = domString
            return div
        }

        class Component {
            constructor(props = {}) {
                this.props = props
            }

            setState(state) {
                const oldEl = this.el;
                this.state = state;
                this._renderDOM()
                if (this.onStateChange) {
                    this.onStateChange(oldEl, this.el)
                }
            }

            _renderDOM() {
                this.el = createDOMFromString(this.render())
                if (this.onClick) {
                    this.el.addEventListener('click', this.onClick.bind(this), false)
                }
                return this.el
            }

        }

        const mount = (component, wrapper) => {
            wrapper.appendChild(component._renderDOM())
            component.onStateChange = (oldEl, newEl) => {
                wrapper.insertBefore(newEl, oldEl)
                wrapper.removeChild(oldEl)
            }
        }

        class LikeButton extends Component {
            constructor(props) {
                super(props)
                this.state = { isLiked: false }
            }
            onClick() {
                this.setState({
                    isLiked: !this.state.isLiked
                })
            }
            render() {
                return ` 
                    <button class='like-btn' style="background-color: ${this.props.bgColor}"> 
                        <span class='like-text'> 
                            ${this.state.isLiked ? '取消' : '点赞'} 
                        </span >
                        <span>👍</span> 
                    </button >
                `
            }
        }

        class RedBlueButton extends Component {
            constructor(props) {
                super(props)
                this.state = {
                    color: 'red'
                }
            }

            onClick() {
                this.setState({
                    color: this.state.color === 'blue' ? 'red' : 'blue'
                })
            }

            render() {
                return `<div style='color: ${this.state.color};'>${this.state.color}</div>`
            }
        }

        const wrapper = document.querySelector('.wrapper')
        // todo： new 自动完成
        mount(new LikeButton({ bgColor: 'red' }), wrapper)
        mount(new RedBlueButton({ bgColor: 'red' }), wrapper) 
    </script>
</body>

</html>